package com.ebt.m.wiki.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.ebt.m.data.entity.NewProductInfo;
import com.ebt.m.wiki.view.ProductItemView1;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Damon.Han on 2016/7/25 0025.
 */
public class ProductListAdapter1 extends BaseAdapter {
    private Set<Integer> selected;
    private List<NewProductInfo> list;
    private int dataType = 1;
    private Context mContext;
    private int mode_edit;

    public ProductListAdapter1(Context context, List<NewProductInfo> list, int dataType) {
        super();
        this.mContext = context;
        selected = new HashSet<Integer>(10);
        this.list = list;
        this.dataType = dataType;
    }

    public void setMode_edit(int mode_edit) {
        this.mode_edit = mode_edit;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public NewProductInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getProductId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProductItemView1 itemView = null;
        if (convertView == null) {
            itemView = new ProductItemView1(mContext, this.dataType);
            convertView = itemView;
            convertView.setTag(itemView);
        } else {
            itemView = (ProductItemView1) convertView.getTag();
        }
        NewProductInfo data = list.get(position);
        // 绑定数据
        boolean isShowBar = false;
        itemView.setProductInfo(data, 2, mode_edit == ProductItemView1.MODE_EDIT, isShowBar);
        itemView.setChecked(selected.contains(data.getProductId()));
        return convertView;
    }

    public void clearSelected() {
        selected.clear();
        notifyDataSetChanged();
    }

    public void addSelect(int productId) {
        selected.add(productId);
        notifyDataSetChanged();
    }

    public void addOrRemoveSelect(int productId) {
        if (selected.contains(productId)) {
            selected.remove(productId);
        } else {
            selected.add(productId);
        }
        notifyDataSetChanged();
    }

    public void removeSelcted(int productId) {
        selected.remove(productId);
    }

    /**
     * 获取选中
     *
     * @return
     */
    public Set<Integer> getSelected() {
        return selected;
    }
}
